# set this symbol to compile with debug information
# and coverage instrumentation
#BUILD = DEBUG
# set this to compile with optimization
BUILD = RELEASE

# set this to compile against OpenSSL AES-128
#LIBRARY = OPENSSL
# set this to compile against PolarSSL AES-128
LIBRARY = POLARSSL
# set this to compile against CyaSSL AES-128
#LIBRARY = CYASSL
# set this to compile with Chemnitz's native XTEA implementation
#LIBRARY = CHEMNITZ

# set this symbol to cause chemnitzd to reject requests
# not originating from localhost
LOCAL = SET

# install prefix
INSTALL_PREFIX = /usr/local

# user and group as which chemnitzd should run
# make sure to get the UID and GID right
CHEMNITZ_USER = chemnitz
CHEMNITZ_GROUP = chemnitz
CHEMNITZ_UID = 1003
CHEMNITZ_GID = 1004

# flags to use when $(DEBUG) is not set
RELEASE_CFLAGS = -O2
# flags to use when $(DEBUG) is set
DEBUG_CFLAGS = -g --coverage -O0 -DDEBUG

# compiler warnings
WARNING_FLAGS = -Wall -Wextra -std=c99 -pedantic
ifeq ($(BUILD), RELEASE)
	WARNING_FLAGS += -Werror
endif

# where to find libchemnitz header files
INCLUDE_DIR = ../include

# compiler to use
CC = gcc

ifeq ($(BUILD), DEBUG)
	CFLAGS = $(DEBUG_CFLAGS)
else
	CFLAGS = $(RELEASE_CFLAGS)
endif
CFLAGS += $(WARNING_FLAGS)
CFLAGS += -I../include
CFLAGS += -D_POSIX_C_SOURCE
CFLAGS += -D'CHEMNITZ_UID=$(CHEMNITZ_UID)'
CFLAGS += -D'CHEMNITZ_GID=$(CHEMNITZ_GID)'
ifeq ($(LOCAL), SET)
	CFLAGS += -DLOCAL
endif

LDFLAGS = -static
LDFLAGS += -L.
LDFLAGS += -lchemnitz

ifeq ($(LIBRARY), OPENSSL)
	CFLAGS += -I../openssl/include
	LDFLAGS += -L../openssl -lcrypto
endif
ifeq ($(LIBRARY), POLARSSL)
	CFLAGS += -I../polarssl/include
	LDFLAGS += -L../polarssl/library -lpolarssl
endif
ifeq ($(LIBRARY), CYASSL)
	CFLAGS += -I../cyassl
	LDFLAGS += -L../cyassl/src/.libs -lcyassl
endif
ifeq ($(LIBRARY), CHEMNITZ)
	CFLAGS += -DXTEA
endif

all:
	CFLAGS="$(CFLAGS)"                 \
	LDFLAGS="$(LDFLAGS)"               \
	INCLUDE_DIR="$(INCLUDE_DIR)"       \
	LIBRARY="$(LIBRARY)"               \
	CC=$(CC)													 \
	make -C src all

new: clean all

clean:
	make -C src clean

ifeq ($(BUILD), DEBUG)
install: install_error
else
install: really_install
endif

test:
	@echo "***Not yet implemented."

install_error:
	@echo "***DEBUG is set. Not doing nothing no way nohow."

really_install: all
	@echo "Install in $(INSTALL_PREFIX)"
	install -m 0754 -t $(INSTALL_PREFIX)/bin src/chemnitzd
	install -m 0754 -t $(INSTALL_PREFIX)/bin src/chemnitz_control
	install -m 0644 -t $(INSTALL_PREFIX)/lib src/libchemnitz.a
	install -m 0755 -o $(CHEMNITZ_USER) -g $(CHEMNITZ_GROUP) -d /var/chemnitz
	ln -T $(INSTALL_PREFIX)/bin/chemnitz_control $(INSTALL_PREFIX)/bin/chemnitz_restart
	ln -T $(INSTALL_PREFIX)/bin/chemnitz_control $(INSTALL_PREFIX)/bin/chemnitz_stop

